<?php
class Post
{
	public $Table="post";
	public $PostID;
	public $NewsTypeID;
	public $Title;
	public $Summary;
	public $Detail;
	public $TitleEng;
	public $SummaryEng;
	public $DetailEng;
	
	public $LastUpdate;
	public $Source;
	public $Viewer;
	public $ImageID;
	public $ImagePath;
	public $Price;
	public $Attachment;
	public $Status;
	public $ListStatus = array(
								'sanphambanchay' =>'Sản phẩm bán chạy',
								'sanphamhot' =>'Sản phẩm hot',
								);
	
	
	function loadByKey($id)
	{
		$ob=new ClassConnect();
		$sql="select * from `".$this->Table."` where PostID='".$id."'";
		$row=$ob->singleData($sql);
		$this->PostID=$row['PostID'];
		$this->NewsTypeID=$row['NewsTypeID'];
		$this->Title=$row['Title'];
		$this->Summary=$row['Summary'];
		$this->Detail=$row['Detail'];
		$this->TitleEng=$row['TitleEng'];
		$this->SummaryEng=$row['SummaryEng'];
		$this->DetailEng=$row['DetailEng'];
		
		$this->LastUpdate=$row['LastUpdate'];
		$this->Source=$row['Source'];
		$this->Viewer=$row['Viewer'];
		$this->ImageID=$row['ImageID'];
		$this->ImagePath=$row['ImagePath'];
		$this->Price=$row['Price'];
		$this->Attachment=$row['Attachment'];
		$this->Status = $row['Status'];
	}
	function loadByKeyLang($id,$lang)
	{
		$ob=new ClassConnect();
		$sql="select * from `".$this->Table."` where PostID='".$id."'";
		$row=$ob->singleData($sql);
		$this->PostID=$row['PostID'];
		$this->NewsTypeID=$row['NewsTypeID'];
		
		$s=str_replace("“" ,"\"", $row['Title'.$lang]);
		$s=str_replace("”" ,"\"", $s);
		$this->Title=$s;
		$this->Summary=$row['Summary'.$lang];
		$this->Detail=$row['Detail'.$lang];
		
		$this->Image=$row['Image'];
		$this->LastUpdate=$row['LastUpdate'];
		$this->Source=$row['Source'];
		$this->Viewer=$row['Viewer'];
		$this->ImageID=$row['ImageID'];
		$this->ImagePath=$row['ImagePath'];
		$this->Price=$row['Price'];
		$this->Attachment=$row['Attachment'];
		$this->Status = $row['Status'];
	}
	function getTable($dk="")
	{
		$ob=new ClassConnect();
		$sql="select * from `".$this->Table."` $dk";
		$data=$ob->mutiData($sql);
		return $data;
	}
	function travel($dk)
	{
		$arr=array();
		$data=$this->getTable($dk);
		for($i=0;$i<count($data);$i++)
			array_push($arr,$data[$i]['PostID']);
		return $arr;
	}
	
	function getByMenu($menuid,$from = 0 , $to = 0 ,$order = "ORDER BY `post`.`LastUpdate` DESC")
	{
		if(!is_array($menuid))
			$where = " WHERE NewsTypeID = '$menuid' ".$order;
		else
		{
			$list = implode("','",$menuid);	
			$where = " WHERE NewsTypeID in ('".$list."') ".$order;
		}
		if($to>0)
			$where .= " LIMIT $from , $to";
		$data=$this->getTable($where);
		return $data;	
	}
	function insert($conn)
	{
		$ob=new ClassConnect();
		$this->PostID = $ob->getNextId($this->Table,"PostID");
		
		
		$dt=new ClassDateTime();
		$this->LastUpdate=$dt->_getNow("-");
		
		$field=array(
						"PostID",
						"NewsTypeID",
						"Title",
						"Summary",
						"Detail",
						"TitleEng",
						"SummaryEng",
						"DetailEng",
						"LastUpdate",
						"Source",
						"ImageID",
						"ImagePath",
						"Price",
						"Attachment",
						"Status"
					);
		$value=array(
						$this->PostID,
						$this->NewsTypeID,
						$this->Title,
						$this->Summary,
						$this->Detail,
						$this->TitleEng,
						$this->SummaryEng,
						$this->DetailEng,
						$this->LastUpdate,
						$this->Source,
						$this->ImageID,
						$this->ImagePath,
						$this->Price,
						$this->Attachment,
						$this->Status
					);
		return $ob->insertData($conn,$this->Table,$field,$value);
	}
	function update($conn)
	{
		$ob=new ClassConnect();
		$obj = new Post();
		$obj->loadByKey($this->PostID);
		
		
		
		$dt=new ClassDateTime();
		$this->LastUpdate=$dt->_getNow("-");
		$field=array(
						"PostID",
						"NewsTypeID",
						"Title",
						"Summary",
						"Detail",
						"TitleEng",
						"SummaryEng",
						"DetailEng",
						"LastUpdate",
						"Source",
						"ImageID",
						"ImagePath",
						"Price",
						"Attachment",
						"Status"
					);
		$value=array(
						$this->PostID,
						$this->NewsTypeID,
						$this->Title,
						$this->Summary,
						$this->Detail,
						$this->TitleEng,
						$this->SummaryEng,
						$this->DetailEng,
						$this->LastUpdate,
						$this->Source,
						$this->ImageID,
						$this->ImagePath,
						$this->Price,
						$this->Attachment,
						$this->Status
					);
		$where="PostID = '".$this->PostID."'";
		return $ob->updateData($conn,$this->Table,$field,$value,$where);			
	}
	
	function updateImage($conn)
	{
		$ob=new ClassConnect();
		
		
		$field=array("Status");
		$value=array("");
		$where="FileID = '".$this->ImageID."'";
		return $ob->updateData($conn,"tblfile",$field,$value,$where);
	}
	
	function delete($conn)
	{
		$ob=new ClassConnect();	
		
		$where="PostID = '".$this->PostID."'";
		return $ob->deleteData($conn,$this->Table,$where);
			
	}
	
	function incViewer($conn)
	{
		$ob=new ClassConnect();
		$field=array("Viewer");
		$value=array($this->Viewer+1);
		$where="PostID = '".$this->PostID."'";
		return $ob->updateData($conn,$this->Table,$field,$value,$where);
	}
	
}
?>
